Note: This tutorial assumes that you have completed the previous tutorials: Using Ecl Formatters. |
![]() |
Writing a Custom Formatter
Description: Writing your own custom formatters.Keywords: ecl formatters
Tutorial Level: INTERMEDIATE
Formatters must implement
- Formatting setters
- Input value operator
- Temporary formatting operators
- Streaming friend function
1 class MatrixFormatter {
2 public:
3 MatrixFormatter& precision(int p); // Setter
4 MatrixFormatter& operator()(Matrix M); // Input Operator
5 MatrixFormatter& operator(int p, int w ... ); // Combination setter
6 MatrixFormatter& operator(Matrix M, int p, int w ...); //Temporary formatting operator
7 template <typename OutputStream, typename N> friend OutputStream& operator << (OutputStream& ostream, MatrixFormatter& formatter);
8 }
Note that the setters and operators must return the formatter so that the friend function can implement the formatting.
For the Format<Type> class to automagically find this formatter, you can implement one of two methods. If you have access to the class's internals, simply typedef it as "Formatter" inside the class, otherwise simply specialise the Format<> class for your type and inherit the formatter.
Example
See include/ecl/containers/array$ vim formatters.hpp in ecl_containers for a concrete example.